<script type="text/javascript">
	$(function() {
		var pg = App.wechat.msgTypeEdit.ui , pp = pg.params;
		var frm = {
			msgCode : pg.seek('msgCode'),
			weChatTemplate : pg.seek('weChatTemplate'),
			status : pg.seek('status'),
			memo : pg.seek('memo')
		};
		var setValues = function (ps) {
			if (ps) {
				frm.msgCode.val(ps.msgCode || '');
				frm.weChatTemplate.val(ps.wechatTemplate || '');
				frm.status.val(ps.status || '');
				frm.memo.val(ps.memo || '');
			}
		};
		var templateGd = $('<div class="grid"></div>').mac('grid', {
			key: 'id',
			cols : [{
				field: 'name', title : '模板名称', width: 150
			}],
			loader: {
				url: App.ctx + '/wechat/list.do',
				params: { pageNo: 1, pageSize: 50 },
				autoLoad: true
			},
			pagerLength: 10
		});
		frm.weChatTemplate.mac('combo', {
			keyField: 'id',
			displayField: 'name',
			multiSelect: false,
			width: 390,
			boxWidth: 390,
			boxHeight: 320,
			boxEl: templateGd
		});
		frm.weChatTemplate.mac('combo_grid', {});
		
		var getValues = function () {
			return {
				msgCode : frm.msgCode.val(),
				weChatTemplateId : frm.weChatTemplate.val(),
				status : frm.status.val(),
				memo : frm.memo.val()
			};
		};
		var validate = function (ps) {
			var msg = pg.seek('msg');
			msg.html('');
			var x = App.validate, v = App.wechat.validation, rt;
			rt = x(v.msg.msgCode, ps.msgCode, msg, "消息编号") 
				&& x(v.msg.weChatTemplate, ps.weChatTemplateId, msg, "微信模板");
			if (!rt) {
				msg.fadeIn('slow');
			}
			return rt;
		};
		pg.seek('save').click(function () {
			var params = getValues();
			if (validate(params)) {
				var action = "/msg/edit.do";
				if (pp) {
					$.extend(params, {id : pp.id || ''});
				} else {
					action = "/msg/add.do";
				}
				
				$.post(App.ctx + action, params, function (rs) {
					var ro = mac.eval(rs);
					if (ro.success) {
						pg.callback(ro.data);
						if(pg.close) {
							pg.close();
						} else {
							console.log('else');
						}
					} else {
						var msg = pg.seek('msg');
						msg.html(mac.msg(ro.data, ro.params));
						msg.fadeIn('slow');
					}
				});
			}
		});
		pg.seek('close').click(function () {
			pg.callback(pp);
			pg.close();
		});
		
		setValues(pp);
	});
</script>
<div class="form">
	<div name="msg" class="msg"></div>
	<div class="item">
		<span class="label">消息编号&nbsp;<span class="msg">*</span></span>
		<input type="text" name="msgCode" placeholder="消息编号">
		<div class="clear"></div>
	</div>
	<div class="item">
		<input type="hidden" name="status" placeholder="发布状态">
	</div>
	<div class="item">
		<span class="label">微信模板&nbsp;<span class="msg">*</span></span>
		<div class="combo fl" name="weChatTemplate"></div>
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">备注&nbsp;</span>
		<textarea rows="3" cols="50" name="memo" style="width: 390px" placeholder="备注"></textarea>
		<div class="clear"></div>
	</div>
	<div class="bottom">
		<button name="save" type="button" class="btn radius btn-white btn-sm">保存</button>
		<button name="close" type="button" class="btn radius btn-info btn-sm">关闭</button>
	</div>
</div>
